
/**
 * Created by Administrator on 2016/7/20.
 */
angular.module('app.services', [])

    .factory('CollegeManager', function($q, $http, $state){
        var pageCount = 10;
        //获取省列表
        var getProvinceList = function($scope){
            //$q是Angular的一种内置服务，它可以使你异步地执行函数，并且当函数执行完成时它允许你使用函数的返回值（或异常）。
            //通过$q服务注册一个延迟对象 deferred
            var d = $q.defer();
            //通过deferred延迟对象，可以得到一个承诺promise，而promise会返回当前任务的完成结果
            var promise = d.promise;
            var info = {
            };
            var postData = {params: JSON.stringify(info)};
            var config = {};
            //接口文档
            $http.post(localStorage.ipAddress + '/get_province_list/', postData, config)
                .success(function(response){
                    var status = response['status'];
                    var data = response['data'];
                    if (status == 'SUCCESS'){
                        // console.log('provinceList',response);
                        $scope.provinceList = data;
                    }else{
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                })
                .error(function(error){
                    console.log(error);
                });
            promise.success = function(fn){
                //捕获未处理的异常然后把错误状态作为返回值返回
                promise.then(fn);
                return promise;
            };
            promise.error = function(fn){
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };
        //根据省ID获取城市列表
        var getCityList = function($scope, provinceID){
            if (provinceID == null){
                provinceID = 0;
            }
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                provinceID:provinceID
            };
            var postData = {params: JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_city_list_by_province/', postData, config)
                .success(function(response){
                    var status = response['status'];
                    var data = response['data'];
                    if (status == 'SUCCESS'){
                        $scope.cityList = data;
                        console.log('get_city_list_by_province数据', data);
                    }else{
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                })
                .error(function(error){
                    console.log(error);
                });
            promise.success = function(fn){
                promise.then(fn);
                return promise;
            };
            promise.error = function(fn){
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };

        //获取招标地区列表
        var getCityListBackground = function($scope){
            var d = $q.defer();
            var promise = d.promise;
            var info = {};
            var postData = {data: JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_city_list/', postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    if (status == 'SUCCESS') {
                        var cityAll = {
                            cityID : '-1',
                            cityName : '全部'
                        };
                        $scope.cityList = [cityAll];
                        $scope.cityList = $scope.cityList.concat(data);
                        d.resolve(data);
                        //console.log("User",$scope.User,data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'TENDER_01'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };


        var getCollegeList = function($scope, cityID){
            if (cityID == null){
                cityID = 0;
            }
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                cityID:cityID
            };
            var postData = {params: JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_all_college_by_city/', postData, config)
                .success(function(response){
                    var status = response['status'];
                    var data = response['data'];
                    if (status == 'SUCCESS'){
                        $scope.collegeList = data;
                    }else{
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                })
                .error(function(error){
                    console.log(error);
                });
            promise.success = function(fn){
                promise.then(fn);
                return promise;
            };
            promise.error = function(fn){
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };

        var setCollegeSupportinfoBackground = function(college, support){
            var info = {
                tokenID : localStorage.tokenID,
                collegeID : college.collegeID,
                isSupport : support
            };
            var d = $q.defer();
            var promise = d.promise;
            var postData = {data : JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress+'/set_college_supportinfo_background/',postData,config)
                .success(function(response){
                    console.log('用户详情',response);
                    var status = response['status'];
                    var data = response['data'];
                    if(status=='SUCCESS'){
                        console.log('操作成功!');
                    }else{
                        college.isSupport = !support;
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                })
                .error(function(error){
                    console.log('发生错误' + error);
                });
            promise.success = function(fn){
                promise.then(fn);
                return promise;
            };
            promise.error = function(fn){
                promise.then(null,fn);
                return promise;
            };
            return d.promise;
        };
        var getCollegeListBriefBackground = function($scope, startIndex){
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                cityID : $scope.data.cityID,
                provinceID : $scope.data.provinceID,
                collegeName : $scope.data.collegeName,
                startIndex : startIndex,
                pageCount : pageCount
            };
            var postData = {data: JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_college_list_brief_background/', postData, config)
                .success(function(response){
                    console.log('学校list', response);
                    var status = response['status'];
                    var data = response['data'];
                    if(status == 'SUCCESS'){
                        $scope.data.collegeList = data;

                        for(var i=0;i<data.length;i++){
                            var u = data[i];
                            u['pushNotificationChange'] = function(x){
                                if(x){
                                    collegeids.push(this.collegeID);
                                    console.log('collegeids数组',collegeids);
                                }else{
                                    collegeids.splice(collegeids.indexOf(this.collegeID), 1);
                                    console.log('collegeids数组',collegeids);
                                }
                            };
                            u['collegeSupportChange'] = function(x){
                                setCollegeSupportinfoBackground(this, x);
                            };
                        }

                        d.resolve(data);
                        //console.log("User",$scope.User,data);
                    }else{
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                })
                .error(function(error){
                    d.reject(error);
                });
            promise.success = function(fn){
                promise.then(fn);
                return promise;
            };
            promise.error = function(fn){
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };
        var createCollegeBackgroud = function($scope){
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                cityID : $scope.data.cityID,
                collegeName : $scope.data.collegeName,
                tokenID : localStorage.tokenID
            };
            var postData = {data: JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/create_college_backgroud/', postData, config)
                .success(function(response){
                    console.log('创建学校', response);
                    var status = response['status'];
                    var data = response['data'];
                    if(status == 'SUCCESS'){
                        d.resolve(data);
                        $state.go("collegemanager");
                    }else{
                        $scope.clickDisable = false;
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                })
                .error(function(error){
                    $scope.clickDisable = false;
                    d.reject(error);
                });
            promise.success = function(fn){
                promise.then(fn);
                return promise;
            };
            promise.error = function(fn){
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };
        var updateCollegeNameBackground = function($scope, collegeID){
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                collegeID : collegeID,
                collegeName : $scope.data.collegeName,
                tokenID : localStorage.tokenID
            };
            var postData = {data: JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/update_college_name_backgroud/', postData, config)
                .success(function(response){
                    console.log('更新学校名称', response);
                    var status = response['status'];
                    var data = response['data'];
                    if(status == 'SUCCESS'){
                        d.resolve(data);
                        $state.go("collegemanager");
                    }else{
                        $scope.data.clickDisable = false;
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                })
                .error(function(error){
                    $scope.data.clickDisable = false;
                    d.reject(error);
                });
            promise.success = function(fn){
                promise.then(fn);
                return promise;
            };
            promise.error = function(fn){
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };
        var getCollegeNameByCollegeIDBackground = function($scope, collegeID){
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                collegeID : collegeID,
                tokenID : localStorage.tokenID
            };
            var postData = {data: JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_college_name_by_college_id_backgroud/', postData, config)
                .success(function(response){
                    console.log('学校 详情', response);
                    var status = response['status'];
                    var data = response['data'];
                    if(status == 'SUCCESS'){
                        $scope.data = data;
                        d.resolve(data);
                    }else{
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                })
                .error(function(error){
                    d.reject(error);
                });
            promise.success = function(fn){
                promise.then(fn);
                return promise;
            };
            promise.error = function(fn){
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };

        return {
            getProvinceList : getProvinceList,
            getCityList : getCityList,
            getCollegeList : getCollegeList,
            setCollegeSupportinfoBackground : setCollegeSupportinfoBackground,
            getCollegeListBriefBackground : getCollegeListBriefBackground,
            createCollegeBackgroud : createCollegeBackgroud,
            updateCollegeNameBackground : updateCollegeNameBackground,
            getCollegeNameByCollegeIDBackground : getCollegeNameByCollegeIDBackground,
            getCityListBackground : getCityListBackground
        };
    })

    .factory('DateTimeManager', function(){
        var formatDatetime = function(){
            Date.prototype.Format = function(fmt)
            { //author: meizz
                var o = {
                    "M+": this.getMonth() + 1, //月份
                    "d+": this.getDate(), //日
                    "h+": this.getHours(), //小时
                    "m+": this.getMinutes(), //分
                    "s+": this.getSeconds(), //秒
                    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                    "S": this.getMilliseconds() //毫秒
                };
                if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
                for (var k in o)
                    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                return fmt;
            };
        };
        var initDate = function($scope, dt){
            formatDatetime();
            $scope.today = function() {
                dt = new Date();
            };
            $scope.today();

            $scope.clear = function() {
                dt = null;
            };

            $scope.inlineOptions = {
                customClass: getDayClass,
                minDate: new Date(),
                showWeeks: true
            };

            $scope.dateOptions = {
                dateDisabled: disabled,
                formatYear: 'yy',
                maxDate: new Date(2020, 5, 22),
                minDate: new Date(),
                startingDay: 1
            };

            // Disable weekend selection
            // getDay() 返回周几, 周日为0, 周六为6
            function disabled(data) {
                var date = data.date;
                var mode = data.mode;
                return mode === 'day' && (false);
            }

            $scope.toggleMin = function() {
                $scope.inlineOptions.minDate = $scope.inlineOptions.minDate ? null : new Date();
                $scope.dateOptions.minDate = $scope.inlineOptions.minDate;
            };

            $scope.toggleMin();

            $scope.open1 = function() {
                $scope.popup1.opened = true;
            };

            $scope.open2 = function() {
                $scope.popup2.opened = true;
            };

            $scope.setDate = function(year, month, day) {
                //dt = new Date(year, month, day);
                dt.setYear(year);
                dt.setMonth(month);
                dt.setDate(day);
            };

            $scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
            $scope.format = $scope.formats[0];
            $scope.altInputFormats = ['M!/d!/yyyy'];

            $scope.popup1 = {
                opened: false
            };

            $scope.popup2 = {
                opened: false
            };

            var tomorrow = new Date();
            tomorrow.setDate(tomorrow.getDate() + 1);
            var afterTomorrow = new Date();
            afterTomorrow.setDate(tomorrow.getDate() + 1);
            $scope.events = [
                {
                    date: tomorrow,
                    status: 'full'
                },
                {
                    date: afterTomorrow,
                    status: 'partially'
                }
            ];

            function getDayClass(data) {
                var date = data.date,
                    mode = data.mode;
                if (mode === 'day') {
                    var dayToCheck = new Date(date).setHours(0,0,0,0);

                    for (var i = 0; i < $scope.events.length; i++) {
                        var currentDay = new Date($scope.events[i].date).setHours(0,0,0,0);

                        if (dayToCheck === currentDay) {
                            return $scope.events[i].status;
                        }
                    }
                }

                return '';
            }
        };
        var initTime = function($scope, t){
            formatDatetime();
            t.mytime = new Date();

            t.hstep = 1;
            t.mstep = 15;

            t.options = {
                hstep: [1, 2, 3],
                mstep: [1, 5, 10, 15, 25, 30]
            };

            t.ismeridian = true;
            t.toggleMode = function() {
                t.ismeridian = ! t.ismeridian;
            };

            //$scope.update = function() {
            //    var d = new Date();
            //    d.setHours( 14 );
            //    d.setMinutes( 0 );
            //    t.mytime = d;
            //};

            t.changed = function () {
                console.log('Time changed to: ' + t.mytime);
            };

            t.clear = function() {
                t.mytime = null;
            };
        };

        return {
            initDate : initDate,
            initTime : initTime
        };
    })

    .factory('SwitchManager', function(){
        var initSwitch = function($scope){
            //$scope.isSelected = 'yep';
            $scope.isSelected = 'nope';
            $scope.onText = 'Y';
            $scope.offText = 'N';
            $scope.isActive = true;
            $scope.size = 'normal';
            $scope.animate = true;
            $scope.radioOff = true;
            $scope.handleWidth = "auto";
            $scope.labelWidth = "auto";
            $scope.inverse = true;

            $scope.$watch('isSelected', function() {
                //$log.info('Selection changed.');
                //console.log('Selection changed.');
            });

            $scope.toggle = function() {
                $scope.isSelected = $scope.isSelected === 'yep' ? 'nope' : 'yep';
            };

            $scope.setUndefined = function() {
                $scope.isSelected = undefined;
            };

            $scope.toggleActivation = function() {
                $scope.isActive = !$scope.isActive;
            };
        };

        return {
            initSwitch : initSwitch
        };
    })

    .factory('AdminManager', function($q, $http, $state){
        var administratorLoginBackground = function($scope){
            var d = $q.defer();
            var promise = d.promise;
            if ($scope.data.adminName == null){
                $scope.data.adminName = '';
            }
            if ($scope.data.password == null){
                $scope.data.password = '';
            }

            var info = {
                adminName : $scope.data.adminName,
                password : $scope.data.password,
                code : 'aa'
            };
            var postData = {data : JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/administrator_login_background/', postData, config)
                .success(function(response){
                    // console.log(response);
                    var status = response['status'];
                    var data = response['data'];
                    if (status == 'SUCCESS'){
                        $state.go('usermanager');
                        localStorage.tokenID = data;
                        d.resolve(data);
                    }else{
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                })
                .error(function(error){
                    console.log(error);
                });
            promise.success = function(fn){
                //捕获未处理的异常然后把错误状态作为返回值返回
                promise.then(fn);
                return promise;
            };
            promise.error = function(fn){
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };

        return {
            administratorLoginBackground : administratorLoginBackground
        };
    })

    .factory('User',function($q, $http, $state){
        var pageCount = 10;
        var userids = [];
        var deleteUser = function(users){
            for(var i=0; i<users.length; i++){
                var user = users[i];
                user.btnDeleteUser = function(){
                    this.clickDisable = true;
                    var r = confirm("确认删除?");
                    if (r != true){
                        this.clickDisable = false;
                        return;
                    }
                    var userID = this.userID;
                    var d = $q.defer();
                    var promise = d.promise;
                    var info = {
                        tokenID : localStorage.tokenID,
                        userIDList : [userID]
                    };
                    var postData = {data: JSON.stringify(info)};
                    var config = {};
                    $http.post(localStorage.ipAddress + '/delete_user_background/', postData, config)
                        .success(function (response) {
                            var status = response['status'];
                            var data = response['data'];
                            if (status == 'SUCCESS') {
                                for(var j=0; j<users.length; j++){
                                    if(users[j].userID == userID){
                                        users.splice(users.indexOf(users[j]), 1);
                                        break;
                                    }
                                }
                                d.resolve(data);
                            }else {
                                alert(data['zhInfo']);
                                if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                                    $state.go('login');
                                }
                                this.clickDisable = false;
                            }
                        })
                        .error(function (error) {
                            this.clickDisable = false;
                            d.reject(error);
                        });
                    promise.success = function (fn) {
                        promise.then(fn);
                        return promise;
                    };
                    promise.error = function (fn) {
                        promise.then(null, fn);
                        return promise;
                    };
                    return d.promise;
                };
            }
        };
        var getUserListBriefBackground = function($scope, userName, startIndex) {
            var d = $q.defer();
            var promise = d.promise;
            var searchKey = null;
            var func_name = '/get_user_list_brief_background/';
            var info = {
                searchKey: userName,
                startIndex: startIndex,
                pageCount: 10
            };
            var postData = {data: JSON.stringify(info)};
            var config = {};
            if ($scope.data.searchKey != null) {
                searchKey = $scope.data.searchKey;
                func_name = '/search_background/';
                info = {
                    startIndex : startIndex,
                    pageCount : pageCount,
                    tokenID : localStorage.tokenID,
                    searchKey : $scope.data.searchKey,
                    tag : 1
                };
                postData = { data : JSON.stringify(info)};
            }
            $http.post(localStorage.ipAddress + func_name, postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    console.log('userList',response);
                    $scope.start = startIndex;
                    if (status == 'SUCCESS') {
                        $scope.users = data;
                        deleteUser($scope.users);
                        if(data.length < 10){
                            $scope.data.nextDisable = true;
                        }else{
                            $scope.data.nextDisable = false;
                        }
                        for(var i=0; i<$scope.users.length; i++){
                            $scope.users[i].clickDisable = false;
                        }

                        d.resolve(data);
                        //console.log("User",$scope.User,data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };
        var getUserAmountBackground = function($scope){
            //console.log(userids);
            var d = $q.defer();
            var promise = d.promise;
            var info = {

                tokenID : localStorage.tokenID

            };
            var postData = {data: JSON.stringify(info)};
            //console.log('userlist', postData);
            var config = {};
            $http.post(localStorage.ipAddress + '/get_user_amount_background/', postData, config)
                .success(function(response){
                    //console.log('用户count',response);
                    var status = response['status'];
                    var data = response['data'];
                    $scope.userAmout  = data;
                    if(status != 'SUCCESS'){
                        console.log('失败')
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                    d.resolve(data);
                })
                .error(function(error){
                    d.reject(error);
                });
            promise.success = function(fn){
                promise.then(fn);
                return promise;
            };
            promise.error = function(fn){
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };
        var getUserDetailBackground = function($scope,userID) {
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                userID: userID,
                tokenID : localStorage.tokenID
            };
            var postData = {data : JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress+'/get_user_info_detail_with_signature_background/',postData,config)
                .success(function(response){
                    console.log('用户详情',response);
                    var status = response['status'];
                    var data = response['data'];
                    if(status=='SUCCESS'){
                         console.log(response);
                        if(data.gender == 1){
                            data.sex = '男';
                        }else if(data.gender == 0){
                            data.sex = '女';
                        }else{
                            data.sex = '未知';
                        }
                        $scope.data.user = data;
                    }else{
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                })
                .error(function(error){
                    console.log(失败+error);
                });
            promise.success = function(fn){
                promise.then(fn);
                return promise;
            };
            promise.error = function(fn){
                promise.then(null,fn);
                return promise;
            };
            return d.promise;
        };
        var deleteUsersBackground = function($scope){
            //console.log(userids);
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                userIDList : userids,
                tokenID : localStorage.tokenID

            };
            var postData = {data: JSON.stringify(info)};
            //console.log('userlist', postData);
            var config = {};
            $http.post(localStorage.ipAddress + '/delete_user_background/', postData, config)
                .success(function(response){
                    console.log('删除用户',response);
                    var status = response['status'];
                    var data = response['data'];
                    //状态success 但是无法删除 mark
                    if(status == 'SUCCESS'){
                        getUserListBriefBackground($scope,'',0);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                    d.resolve(data);
                })
                .error(function(error){
                    d.reject(error);
                });
            promise.success = function(fn){
                promise.then(fn);
                return promise;
            };
            promise.error = function(fn){
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };
        var getMerchandiseWithSignatureListByUserIDBackground = function($scope, userID, startIndex){
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                tokenID: localStorage.tokenID,
                startIndex: startIndex,
                pageCount: pageCount,
                userID : userID
            };
            var postData = {data: JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_merchandise_with_signature_list_by_userID_background/', postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    console.log('user merchandise list',response);
                    if (status == 'SUCCESS') {
                        $scope.data.merchandiseList = data;
                        if(data.length < 10){
                            $scope.data.nextDisable = true;
                        }else{
                            $scope.data.nextDisable = false;
                        }
                        d.resolve(data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };
        var getAccountHistoryListBackground = function($scope, userID, startIndex){
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                tokenID: localStorage.tokenID,
                startIndex: startIndex,
                pageCount: pageCount,
                userID : userID
            };
            var postData = {data: JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_account_history_list_background/', postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    console.log('user account list', response);
                    if (status == 'SUCCESS') {
                        $scope.data.accountList = data.accountHistoryList;
                        $scope.data.accountInfo = data.accountInfo;
                        if(data.accountHistoryList.length < 10){
                            $scope.data.nextDisable = true;
                        }else{
                            $scope.data.nextDisable = false;
                        }
                        d.resolve(data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };
        var getScoreListBackground = function($scope, userID, startIndex){
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                tokenID: localStorage.tokenID,
                startIndex: startIndex,
                pageCount: pageCount,
                userID : userID
            };
            var postData = {data: JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_score_list_history_background/', postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    console.log('user score list',response);
                    if (status == 'SUCCESS') {
                        $scope.data.scoreList = data;
                        if(data.length < 10){
                            $scope.data.nextDisable = true;
                        }else{
                            $scope.data.nextDisable = false;
                        }
                        d.resolve(data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };
        var getSharedListByUserIDBackground = function($scope, userID, startIndex){
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                tokenID: localStorage.tokenID,
                startIndex: startIndex,
                pageCount: pageCount,
                userID : userID
            };
            var postData = {data: JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_shared_list_by_userID_background/', postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    console.log('user shared list',response);
                    if (status == 'SUCCESS') {
                        $scope.data.sharedList = data;
                        if(data.length < 10){
                            $scope.data.nextDisable = true;
                        }else{
                            $scope.data.nextDisable = false;
                        }
                        d.resolve(data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };
        var getArticleListByUserIDBackground = function($scope, userID, startIndex){
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                tokenID: localStorage.tokenID,
                startIndex: startIndex,
                pageCount: pageCount,
                userID : userID
            };
            var postData = {data: JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_article_list_by_user_id_background/', postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    console.log('user article list',response);
                    if (status == 'SUCCESS') {
                        $scope.data.articleList = data;
                        if(data.length < 10){
                            $scope.data.nextDisable = true;
                        }else{
                            $scope.data.nextDisable = false;
                        }
                        d.resolve(data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };
        var getCertificationBackground = function($scope, userID){
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                tokenID: localStorage.tokenID,
                userID : userID
            };
            var postData = {data: JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_certification_background/', postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    console.log('user certificationInfo ', data);
                    if (status == 'SUCCESS') {
                        $scope.data.certificationInfo = data;
                        d.resolve(data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };
        var getAddressListByUserIDBackground = function($scope, userID, startIndex){
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                tokenID: localStorage.tokenID,
                startIndex: startIndex,
                pageCount: pageCount,
                userID : userID
            };
            var postData = {data: JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_address_list_by_user_id_background/', postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    console.log('user address list',response);
                    if (status == 'SUCCESS') {
                        $scope.data.addressList = data;
                        if(data.length < 10){
                            $scope.data.nextDisable = true;
                        }else{
                            $scope.data.nextDisable = false;
                        }
                        d.resolve(data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };
        var getCouponListByUserIDBackground = function($scope, userID, startIndex){
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                tokenID: localStorage.tokenID,
                startIndex: startIndex,
                pageCount: pageCount,
                userID : userID
            };
            var postData = {data: JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_coupon_list_by_userID_background/', postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    console.log('user coupon list',response);
                    if (status == 'SUCCESS') {
                        $scope.data.couponList = data;
                        if(data.length < 10){
                            $scope.data.nextDisable = true;
                        }else{
                            $scope.data.nextDisable = false;
                        }
                        d.resolve(data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };
        var confirmRealNameAuthenticationBackground = function($scope, userID, certificateStep){
            var d = $q.defer();
            var promise = d.promise;
            if($scope.data.certificateDescription == undefined ||
                $scope.data.certificateDescription == null){
                $scope.data.certificateDescription = "";
            }
            var info = {
                tokenID: localStorage.tokenID,
                userID : userID,
                certificateStep : certificateStep,
                certificateDescription : $scope.data.certificateDescription
            };
            var postData = {data: JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/confirm_real_name_authentication_background/', postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    console.log('confirm_real_name_authentication_background', data);
                    if (status == 'SUCCESS') {
                        $scope.data.certificationInfo.certificateStep = certificateStep;
                        d.resolve(data);
                    } else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };
        var getSellerReplyListByUserIDBackground = function($scope, userID, startIndex){
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                tokenID: localStorage.tokenID,
                startIndex: startIndex,
                pageCount: pageCount,
                userID : userID
            };
            var postData = {data: JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_seller_reply_list_by_user_id_background/', postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    console.log('user received reply list',response);
                    if (status == 'SUCCESS') {
                        $scope.data.replyList = data;
                        if(data.length < 10){
                            $scope.data.nextDisable = true;
                        }else{
                            $scope.data.nextDisable = false;
                        }
                        d.resolve(data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };
        var getUserListBackground = function($scope, startIndex){
            var d = $q.defer();
            var promise = d.promise;
            var func_name = '/get_user_list_background/';
            if($scope.data.searchKey != null){
                searchKey = $scope.data.searchKey;
                func_name = '/search_background/';
                info = {
                    startIndex : startIndex,
                    pageCount : pageCount,
                    tokenID : localStorage.tokenID,
                    searchKey : searchKey,
                    tag : 1
                };
                postData = {data : JSON.stringify(info)};
            }
            if($scope.data.cityID == null){
                cityID = '-1';
            }
            var info = {
                startIndex : startIndex,
                pageCount : pageCount,
                tokenID : localStorage.tokenID
            };
            var postData = {data : JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + func_name, postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    $scope.start = startIndex;
                    if (status == 'SUCCESS') {
                        console.log(data);
                        $scope.data.dataList = data.dataList;
                        $scope.data.count = data.count;
                        $scope.data.prevDisable = (startIndex <= 0);
                        $scope.data.nextDisable = (data.count <= (startIndex + pageCount));
                        console.log("userlist111:",$scope.data);
                        d.resolve(data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'TENDER_01'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;

        };

        return{
            getUserListBriefBackground : function($scope, userName){
                getUserListBriefBackground($scope, userName,0);
            },
            getUserPagelist : getUserListBriefBackground,
            getUserAmountBackground : getUserAmountBackground,
            getUserDetailBackground : getUserDetailBackground,
            deleteUsersBackground : deleteUsersBackground,
            getMerchandiseWithSignatureListByUserIDBackground : getMerchandiseWithSignatureListByUserIDBackground,
            getAccountHistoryListBackground : getAccountHistoryListBackground,
            getScoreListBackground : getScoreListBackground,
            getSharedListByUserIDBackground : getSharedListByUserIDBackground,
            getArticleListByUserIDBackground : getArticleListByUserIDBackground,
            getCertificationBackground : getCertificationBackground,
            getAddressListByUserIDBackground : getAddressListByUserIDBackground,
            getCouponListByUserIDBackground : getCouponListByUserIDBackground,
            confirmRealNameAuthenticationBackground : confirmRealNameAuthenticationBackground,
            getSellerReplyListByUserIDBackground : getSellerReplyListByUserIDBackground,
            getUserListBackground : getUserListBackground
        }
    })

    .factory('TenderManager', function($q, $http, $state){
        var pageCount = 10;
        var getTenderListBackground = function($scope, startIndex, startDate, endDate){
            var d = $q.defer();
            var promise = d.promise;
            var searchKey = null;
            var func_name = '/get_tender_list_background/';
            var info = {
                startIndex : startIndex,
                pageCount : pageCount,
                startDate : startDate ? startDate : '-1',
                endDate : endDate ? endDate : '-1',
                cityID : $scope.data.cityID,
                tokenID : localStorage.tokenID
            };
            var postData = {data : JSON.stringify(info)};
            var config = {};
            if($scope.data.searchKey != null && $scope.data.searchKey != ''){
                searchKey = $scope.data.searchKey;
                func_name = '/search_background/';
                info = {
                    startIndex : startIndex,
                    pageCount : pageCount,
                    tokenID : localStorage.tokenID,
                    searchKey : searchKey,
                    tag : 2
                };
                postData = {data : JSON.stringify(info)};
            }

            if($scope.data.cityID == null){
                $scope.data.cityID = '-1';
            }

            $http.post(localStorage.ipAddress + func_name, postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    // $scope.start = startIndex;
                    if (status == 'SUCCESS') {
                        $scope.data.tenderList = data.dataList;
                        $scope.data.count = data.count;

                        $scope.data.nextDisable = ($scope.data.count <= (startIndex + pageCount));
                        $scope.data.prevDisable = (startIndex <= 0);
                        console.log("tenderL", data.dataList);
                        d.resolve(data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'TENDER_01'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };
        var getTenderDetailBackground = function ($scope, tenderID) {
            var d = $q.defer();
            var promise = d.promise;

            var info = {
                tenderID : tenderID,
                tokenID : localStorage.tokenID
            };
            var postData = {data : JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_tender_detail_background/', postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    console.log('userList',response);
                    if (status == 'SUCCESS') {
                        console.log("data", data);
                        $scope.data = data;
                        var ifrm = document.getElementById('detail');
                        ifrm.contentWindow.document.write(data.detail);
                        if(ifrm.contentWindow.document.getElementsByTagName('body').length != 0){
                            // ifrm.contentWindow.document.getElementsByTagName('body')[0].style.marginLeft = '280px'
                        }
                        ifrm.style.width = "100%";
                        ifrm.style.height = "800px";
                        d.resolve(data);
                        //console.log("User",$scope.User,data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'TENDER_01'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };

        return {
            getTenderListBackground : getTenderListBackground,
            getTenderDetailBackground : getTenderDetailBackground
        };
    })

    .factory('WinbiddingManager', function($q, $http, $state){
        var pageCount = 10;
        var getBiddingListBackground = function($scope, startIndex, startDate, endDate){
            var d = $q.defer();
            var promise = d.promise;
            var searchKey = null;
            var func_name = '/get_bidding_list_background/';
            var info = {
                startIndex : startIndex,
                pageCount : pageCount,
                startDate : startDate ? startDate : '-1',
                endDate : endDate ? endDate : '-1',
                cityID: $scope.data.cityID,
                tokenID : localStorage.tokenID
            };
            console.log("woxiangdayin的info", info);
            var postData = {data : JSON.stringify(info)};
            var config = {};
            if($scope.data.searchKey != null && $scope.data.searchKey != ''){
                searchKey = $scope.data.searchKey;
                func_name = '/search_background/';
                info = {
                    startIndex : startIndex,
                    pageCount : pageCount,
                    tokenID : localStorage.tokenID,
                    searchKey : searchKey,
                    tag : 3
                };
                postData = {data : JSON.stringify(info)};
            }
            var cityID = null;
            if($scope.data.cityID == null){
                $scope.data.cityID = '-1';
            }

            $http.post(localStorage.ipAddress + func_name, postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    $scope.start = startIndex;
                    if (status == 'SUCCESS') {
                        $scope.data.winbiddingList = data.dataList;
                        $scope.data.count = data.count;
                        $scope.data.nextDisable = ($scope.data.count <= (startIndex + pageCount));
                        $scope.data.prevDisable = (startIndex <= 0);
                        console.log("WinbiddingList1258:",data);
                        d.resolve(data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'TENDER_01'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };

        var getBiddingDetailBackground = function($scope, biddingID){
            var d = $q.defer();
            var promise = d.promise;

            var info = {
                biddingID : biddingID,
                tokenID : localStorage.tokenID
            };
            var postData = {data : JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_bidding_detail_background/', postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    console.log('userList',response);
                    if (status == 'SUCCESS') {
                        // console.log("winbiddingData", data);
                        $scope.data = data;
                        var ifrm = document.getElementById('detail');
                        ifrm.contentWindow.document.write(data.detail);
                        if(ifrm.contentWindow.document.getElementsByTagName('body').length != 0){
                            ifrm.contentWindow.document.getElementsByTagName('body')[0].style.marginLeft = '280px'
                        }
                        ifrm.style.width = "100%";
                        ifrm.style.height = '800px';
                        d.resolve(data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'TENDER_01'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };

        return {
            getBiddingListBackground : getBiddingListBackground,
            getBiddingDetailBackground : getBiddingDetailBackground
        };
    })

    .factory('NewsManager', function ($q, $http, $state) {

        var pageCount = 2;
        var index = 0;
        // 获取招标咨询列表
        var getNewsListBackground = function($scope, startIndex){

            var d = $q.defer();
            var promise = d.promise;
            var info = {
                startIndex : startIndex,
                pageCount : pageCount,
                tokenID : localStorage.tokenID
            };
            index = startIndex;

            var postData = {data : JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_news_list_background/', postData, config)
                .success(function(response){
                    var status = response['status'];
                    var data = response['data'];
                    if(status == 'SUCCESS'){
                        console.log("/get_news_list_background/", data);

                        $scope.data.tendernewsList = data.dataList;
                        $scope.data.count = data.count;
                        $scope.data.nextDisable = (data.count <= (pageCount+startIndex));
                        $scope.data.prevDisable = (startIndex <= 0);
                        d.resolve(data);
                    }else{
                        alert(data['zhInfo']);
                        if(data['code'] == 'TENDER_01'){
                            $state.go('login');
                        }
                    }
                })
                .error(function(error){
                    d.reject(error);
                });
            promise.success = function(fn){
                promise.then(fn);
                return promise;
            };
            promise.error = function(fn){
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };


        // 删除招标咨询
        var deleteNewsBackground = function($scope, newsID){
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                newsID : newsID,
                tokenID : localStorage.tokenID
            };
            var postData = {data : JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/delete_news_background/', postData, config)
                .success(function(response){
                    var status = response['status'];
                    var data = response['data'];
                    if(status == 'SUCCESS'){
                        console.log("/delete_news_background/", data);
                        getNewsListBackground($scope,index);
                        alert('删除成功');
                        d.resolve(data);
                    }else{
                        alert(data['zhInfo']);
                        if(data['code'] == 'TENDER_01'){
                            $state.go('login');
                        }
                    }
                })
                .error(function(error){
                    d.reject(error);
                });
            promise.success = function(fn){
                promise.then(fn);
                return promise;
            };
            promise.error = function(fn){
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };
        //新建招标咨询
        var createNews = function($scope){
            var d = $q.defer();
            var promise = d.promise;
            var fd = new FormData();
            var imgList = [];
            for(var i=0; i<$scope.data.flow.files.length; i++){
                var _imgName = hex_md5($scope.data.flow.files[i].name);
                fd.append(_imgName, $scope.data.flow.files[i].file);
                //var _url = '-1';
                //if($scope.data.flow.files[i].url != undefined && $scope.data.flow.files[i].url != null){
                //    _url = $scope.data.flow.files[i].url;
                //}
                imgList.push({
                    imgName : _imgName
                });
            }

            if(imgList.length <= 0){
                alert('请上传图片');
                return;
            }else {
                var info = {
                    tokenID : localStorage.tokenID,
                    imgNameList : imgList,
                    title : $scope.data.title,
                    content : $scope.data.content
                };
                fd.append('data', JSON.stringify(info));
                var postData = {data: JSON.stringify(info)};
                var config = {
                    transformRequest: angular.identity,
                    headers: {'Content-Type': undefined}
                };
                console.log(postData);
                $http.post(localStorage.ipAddress + '/create_news/', fd, config)
                    .success(function(response){
                        var status = response['status'];
                        var data = response['data'];
                        if(status == 'SUCCESS'){
                            console.log("file-data-resp",data);
                            $state.go("tendernews");
                            d.resolve(data);
                        }else {
                            alert(data['zhInfo']);
                            if (data['code'] == 'SPORTS_01' || data['code'] == 'SPORTS_09'){
                                $state.go('login');
                            }
                        }
                    })
                    .error(function(error){
                        d.reject(error);
                    });
                promise.success = function(fn){
                    promise.then(fn);
                    return promise;
                };
                promise.error = function(fn){
                    promise.then(null, fn);
                    return promise;
                };
                return d.promise;
            }
        };

        // 获取招标咨询详情
        var getNewsDetailBackground = function($scope, newsID){
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                newsID : newsID,
                tokenID : localStorage.tokenID
            };
            var postData = {data : JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_news_detail_background/', postData, config)
                .success(function(response){
                    var status = response['status'];
                    var data = response['data'];
                    if(status == 'SUCCESS'){
                        console.log("/get_news_detail_background/", data);
                        $scope.data = data;
                        d.resolve(data);
                    }else{
                        alert(data['zhInfo']);
                        if(data['code'] == 'TENDER_01'){
                            $state.go('login');
                        }
                    }
                })
                .error(function(error){
                    d.reject(error);
                });
            promise.success = function(fn){
                promise.then(fn);
                return promise;
            };
            promise.error = function(fn){
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };
        return{
            createNews : createNews,
            getNewsListBackground : getNewsListBackground,
            getNewsDetailBackground : getNewsDetailBackground,
            deleteNewsBackground : deleteNewsBackground
        }
    })

    .factory('CompanyManager', function($q, $http, $state){
        var pageCount = 10;
        var getCompanyListBackground = function($scope, startIndex){
            var d = $q.defer();
            var promise = d.promise;
            var searchKey = null;
            var func_name = '/get_company_list_background/';
            if($scope.data.searchKey != null){
                searchKey = $scope.data.searchKey;
                func_name = 'get_company_list_background';
            }
            if($scope.data.cityID == null){
                $scope.data.cityID = -1;
            }

            var info = {
                startIndex : startIndex,
                pageCount : pageCount,
                cityID : $scope.data.cityID,
                tokenID : localStorage.tokenID
            };
            var postData = {data : JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + func_name, postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    $scope.start = startIndex;
                    if (status == 'SUCCESS') {
                        $scope.data.companys = data.dataList;
                        $scope.data.count = data.count;
                        $scope.data.nextDisable = ($scope.data.count <= (startIndex + pageCount));
                        $scope.data.prevDisable = (startIndex <= 0);
                        d.resolve(data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'TENDER_01'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };

        var getCompanyDetailBackground = function($scope, companyID){
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                companyID : companyID,
                tokenID : localStorage.tokenID
            };
            var postData = {data : JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_company_detail_background/', postData, config)
                .success(function (response){
                    var status = response['status'];
                    var data = response['data'];
                    if(status == 'SUCCESS') {
                        $scope.companydetail = data;
                        // console.log("$scope.companydetail",$scope.companydetail);
                        d.resolve(data);
                    }else{
                        alert(data['zhInfo']);
                        if(data['code'] == 'TENDER_01'){
                            $state.go('login');
                        }
                    }
                })
                .error(function(error){
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };

        var getCompanyImgBackground = function($scope, startIndex){
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                companyID : $scope.data.companyID,
                tag : $scope.data.tag,
                tokenID : localStorage.tokenID,
                startIndex : startIndex,
                pageCount : 1
            };
            var postData = { data : JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_company_img_background/', postData, config)
                .success(function(response){
                    var status = response['status'];
                    var data = response['data'];
                    if(status == 'SUCCESS'){
                        $scope.data.prevDisable = (1 == (startIndex + 1));
                        $scope.data.nextDisable = (data.count == (startIndex + 1));
                        $scope.data.companyImgList = data.imgList;
                        console.log("imgData::",data.imgList);
                        d.resolve(data);
                    }else{
                        alert(data['zhInfo']);
                        if(data['code'] == 'TENDER_01'){
                            $state.go('login');
                        }
                    }
                })
                .error(function(error){
                    d.reject(error);
                });
            promise.success = function(fn){
                promise.then(fn);
                return promise;
            };
            promise.error = function(fn){
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };

        var getProjectManagerListBackground = function($scope, companyID, startIndex){
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                companyID : companyID,
                startIndex : startIndex,
                pageCount : pageCount
            };
            var postData = {data : JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_project_manager_list_background/', postData, config)
                .success(function(response){
                    var status = response['status'];
                    var data = response['data'];
                    if(status == 'SUCCESS'){
                        $scope.data.managerList = data.dataList;
                        $scope.data.count = data.count;
                        $scope.data.nextDisable = (data.count <= (pageCount+startIndex));
                        $scope.data.prevDisable = (startIndex <= 0);
                        // console.log("dad",$scope.data.managerList);
                        $.each($scope.data.managerList, function(key, value){
                            if(value.gender == 0){
                                value.gender = "女";
                            }else if(value.gender == 1){
                                value.gender = "男";
                            }
                        });
                        d.resolve(data);
                    }else{
                        alert(data['zhInfo']);
                        if(data['code'] == 'TENDER_01'){
                            $state.go('login');
                        }
                    }
                })
                .error(function(error){
                    d.reject(error);
                });
            promise.success = function(fn){
                promise.then(fn);
                return promise;
            };
            promise.error = function(fn){
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };

        var getCompanyAchievementListBackground = function($scope, companyID, startIndex, tag){
            var d = $q.defer();
            var promise = d.promise;
            var func_name = '/get_company_achievement_list_background/';
            var info = {
                companyID : companyID,
                startIndex : startIndex,
                pageCount : pageCount,
                tag : tag,
                tokenID : localStorage.tokenID
            };
            var postData = { data : JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + func_name, postData, config)
                .success(function(response){
                    var status = response['status'];
                    var data = response['data'];
                    console.log("achieve123:", data);
                    if(status == 'SUCCESS'){
                        if(tag == 0){
                            $scope.data.count = data.count;
                            $scope.data.achievePrevDisable = (startIndex <= 0);
                            $scope.data.achieveNextDisable = (data.count <= (startIndex + pageCount));
                            $scope.data.achieveList = data.dataList;
                            console.log("achieveList", $scope.data.achieveList);
                        }else{
                            $scope.data.count = data.count;
                            $scope.data.selfAchievePrevDisable = (startIndex <= 0);
                            $scope.data.selfAchieveNextDisable = (data.count <= (startIndex + pageCount));
                            $scope.data.selfAchieveList = data.dataList;
                            console.log("selefAchieveList", $scope.data.selefAchieveList);
                        }
                        d.resolve(data);
                    }else{
                        alert(data['zhInfo']);
                        if(data['code'] == 'TENDER_01'){
                            $state.go('login');
                        }
                    }
                })
                .error(function(error){
                    d.reject(error);
                });
            promise.success = function(fn){
                promise.then(fn);
                return promise;
            };
            promise.error = function(fn){
                promise.then(null, fn);
                return promise;
            };
            return promise;
        };

        var getDelinquenentConductListBackground = function($scope, companyID, startIndex){
            var d= $q.defer();
            var promise = d.promise;
            var info = {
                companyID : companyID,
                startIndex : startIndex,
                pageCount : pageCount
            };
            var postData = {data : JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_delinquenent_conduct_list_background/', postData, config)
                .success(function(response){
                    var status = response['status'];
                    var data = response['data'];
                    if(status == 'SUCCESS'){
                        $scope.data.managerList = data.dataList;
                        $scope.data.count = data.count;
                        $scope.data.conductNextDisable = (data.count <= (startIndex + pageCount));
                        $scope.data.conductPrevDisable = (startIndex <= 0);
                        console.log("companydeliquenent", data);
                        d.resolve(data);
                    }else{
                        alert(data['zhInfo']);
                        if(data['code'] == 'TENDER_01'){
                            $state.go('login');
                        }
                    }
                })
                .error(function(error){
                    d.reject(error);
                });
            promise.success = function(fn){
                promise.then(fn);
                return promise;
            };
            promise.error = function(fn){
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };

        var getGrade1ListBackground = function($scope){
            var d = $q.defer();
            var promise = d.promise;
            var func_name = '/get_grade_1_list_background/';
            var info = {
                tokenID : localStorage.tokenID
            };
            var postData = {data : JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + func_name, postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    if (status == 'SUCCESS') {
                        $scope.data.domList = data;
                        console.log("comdata首层1:",data);
                        d.resolve(data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'TENDER_01'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };

        var getGrade2ListBackground = function($scope){
            var d = $q.defer();
            var promise = d.promise;
            var func_name = '/get_grade_2_list_background/';
            var info = {
                tokenID : localStorage.tokenID,
                gradeID : localStorage.gradeID1
            };
            var postData = {data : JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + func_name, postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    if (status == 'SUCCESS') {
                        $scope.data.childDomList = data;
                        console.log("comdata第二层:",data);
                        angular.element(".parentDom > li").eq($scope.data.index).children('ul').toggleClass("active");
                        d.resolve(data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'TENDER_01'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };

        var getGrade3ListBackground = function($scope){
            var d = $q.defer();
            var promise = d.promise;
            var func_name = '/get_grade_3_list_background/';
            var info = {
                tokenID : localStorage.tokenID,
                gradeID : localStorage.gradeID2
            };
            var postData = {data : JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + func_name, postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    if (status == 'SUCCESS') {

                        $scope.data.secondChildDomList = data;
                        console.log("comdata第三层:",data);
                        d.resolve(data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'TENDER_01'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };

        var getGrade4ListBackground = function($scope){
            var d = $q.defer();
            var promise = d.promise;
            var func_name = '/get_grade_4_list_background/';
            var info = {
                tokenID : localStorage.tokenID,
                gradeID : localStorage.gradeID3
            };
            console.log("fjdls-info:", info);
            var postData = {data : JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + func_name, postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    if (status == 'SUCCESS') {
                        // $scope.data.thirdChildDomList = data;
                        console.log("comdata第四层:",data);
                        d.resolve(data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'TENDER_01'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };

        return {
            getCompanyListBackground : getCompanyListBackground,
            getCompanyDetailBackground : getCompanyDetailBackground,
            getProjectManagerListBackground : getProjectManagerListBackground,
            getDelinquenentConductListBackground : getDelinquenentConductListBackground,
            getCompanyImgBackground : getCompanyImgBackground,
            getCompanyAchievementListBackground : getCompanyAchievementListBackground,
            getGrade1ListBackground : getGrade1ListBackground,
            getGrade2ListBackground : getGrade2ListBackground,
            getGrade3ListBackground : getGrade3ListBackground,
            getGrade4ListBackground : getGrade4ListBackground
        }
    })

    .factory('TenderDatabaseManager', function($q, $http, $state){
        var pageCount = 10;

        var getCompanyAssistantListBackground = function($scope, startIndex){
            var d = $q.defer();
            var promise = d.promise;
            var searchKey = null;
            var func_name = '/get_company_assistant_list_background/';
            if($scope.data.searchKey != null){
                searchKey = $scope.data.searchKey;
                func_name = '/get_company_assistant_list_background/';
            }

            var info = {
                startIndex : startIndex,
                pageCount : pageCount,
                tokenID : localStorage.tokenID
            };
            var postData = {data : JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + func_name, postData, config)
                .success(function (response) {
                    var status = response['status'];
                    var data = response['data'];
                    $scope.start = startIndex;
                    if (status == 'SUCCESS') {
                        $scope.data.tenderDatabase = data.dataList;
                        $scope.data.count = data.count;
                        $scope.data.nextDisable = ($scope.data.count <= (startIndex + pageCount));
                        $scope.data.prevDisable = (startIndex <= 0);
                        console.log("tenderBaseL: ", data);
                        d.resolve(data);
                    }else {
                        alert(data['zhInfo']);
                        if (data['code'] == 'TENDER_01'){
                            $state.go('login');
                        }
                    }
                })
                .error(function (error) {
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;

        };

        var getBiddingListByCompanyIdBackground = function($scope, companyID, startIndex){
            var d = $q.defer();
            var promise = d.promise;
            var info = {
                companyID : companyID,
                startIndex : startIndex,
                pageCount : pageCount,
                tokenID : localStorage.tokenID
            };
            var postData = {data : JSON.stringify(info)};
            var config = {};
            $http.post(localStorage.ipAddress + '/get_bidding_list_by_company_id_background/', postData, config)
                .success(function(response){
                    var status = response['status'];
                    var data = response['data'];
                    if(status == 'SUCCESS'){
                        $scope.data.tenderList = data.dataList;
                        $scope.data.count = data.count;
                        $scope.data.nextDisable = (data.count <= (pageCount+startIndex));
                        $scope.data.prevDisable = (startIndex <= 0);
                        console.log("/get_bidding_list_by_company_id_background/", data);
                        d.resolve(data);
                    }else{
                        alert(data['zhInfo']);
                        if(data['code'] == 'TENDER_01'){
                            $state.go('login');
                        }
                    }
                })
                .error(function(error){
                    d.reject(error);
                });
            promise.success = function(fn){
                promise.then(fn);
                return promise;
            };
            promise.error = function(fn){
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        }

        return {
            getCompanyAssistantListBackground : getCompanyAssistantListBackground,
            getBiddingListByCompanyIdBackground : getBiddingListByCompanyIdBackground
        }
    })

    .factory('ProjectDatabaseManager', function($q, $http, $state){
        var pageCount = 10;
        var getProjectDatabaseListBackground = function($scope, startIndex){
            var d = $q.defer();
            var promise = d.promise;
            var searchKey = null;
            var func_name = '//';
            var info = {};
            var postData = { data : JSON.stringify(info)};
            var config = {};
            if($scope.data.searchKey != null){
                searchKey = $scope.data.searchKey;
                func_name = '/search_background/';
                info = {};
                postData = { data : JSON.stringify(info)};
            };

            var cityID  = null;
            if($scope.data.cityID == null){
                $scope.data.cityID = '-1';
            };

            $http.post(localStorage.ipAddress + func_name, postData, config)
                .success(function(response){
                    var status = response['status'];
                    var data = response['data'];
                    $scope.start = startIndex;
                    if(status == 'SUCCESS') {
                        $scope.data.count = data.count;
                        $scope.data.nextDisable = ($scope.data.count <= (startIndex + pageCount));
                        $scope.data.prevDisable = (startIndex <= 0);
                        console.log("projectDatabsase", data);
                        d.resolve(data);
                    }else {
                        alert(data['zhInfo']);
                        if(data['code'] == 'TENDER_01') {
                            $state.go('login');
                        }
                    }
                })
                .error(function(error){
                    d.reject(error);
                });
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return d.promise;
        };

        return {
            getProjectDatabaseListBackground: getProjectDatabaseListBackground
        }
    })
;